package com.tian.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tian.mapper.DoctorsMapper;
import com.tian.pojo.Doctors;
import com.tian.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class DoctorsServiceImpl implements DoctorsService{
    @Autowired
    private DoctorsMapper doctorsMapper;
    @Override
    public PageResult getList(PageResult pageResult) {
        //1.定义分页对象
        IPage<Doctors> page = new Page(pageResult.getPageNum(),pageResult.getPageSize());
        //构建查询的条件构造器
        QueryWrapper<Doctors> queryWrapper = new QueryWrapper<>();
        //根据判断条件决定是否拼接where条件
        queryWrapper.like(StringUtils.hasLength(pageResult.getQuery()),"name", pageResult.getQuery());
        //MP提供的分页查询方法,返回值是Page分页对象 其中包含分页的数据结果信息
        page = doctorsMapper.selectPage(page,queryWrapper);
        long total = page.getTotal();
        List<Doctors> records = page.getRecords();
        return pageResult.setRows(records).setTotal(total);

    }

    @Override
    public void deleteById(Integer id) {
        doctorsMapper.deleteById(id);
    }

    @Override
    public List<Doctors> findDoctorList(String hospital) {

        QueryWrapper<Doctors> doctorsQueryWrapper = new QueryWrapper<>();
        doctorsQueryWrapper = doctorsQueryWrapper.eq("hospital", hospital);
        List<Doctors> doctors = doctorsMapper.selectList(doctorsQueryWrapper);
        System.out.println(doctors);
        System.out.println(hospital);
        return doctors;
    }


}
